package com.ha8.netease.views.team.search
{
	import com.ha8.netease.AppFacade;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.models.WeiboProxy;
	import com.ha8.netease.views.utils.DataUtil;
	import com.ha8.netease.views.utils.ObjectUtil;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.MouseEvent;
	
	public class SearchViewMediator extends Mediator
	{
		public function SearchViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'searchViewMediator';
		
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			view.searchBtn.addEventListener(MouseEvent.CLICK, searchEvt);
		}
		
		override public function listNotificationInterests():Array
		{
			return [PrivateNotify.GET_SEARCH_USER_RESULT, PrivateNotify.GET_SEARCH_USER_ERROR,
					PrivateNotify.GET_SEARCH_WEIBO_RESULT, PrivateNotify.GET_SEARCH_WEIBO_ERROR,
					PrivateNotify.GET_SEARCH_WEIBO_NEW_RESULT,
					PrivateNotify.GET_SEARCH_WEIBO_NEW_ERROR];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch(notification.name)
			{
				case PrivateNotify.GET_SEARCH_USER_RESULT:
					view.currentState = "secondPage";
					view.showSearchList();
					view.userDataGroup.dataProvider = DataUtil.ObjToUsers(notification.param["results"]);
					view.showTotal.text = "共有"+notification.param["total"]+"位用户";
					break;
				case PrivateNotify.GET_SEARCH_WEIBO_RESULT:
					view.currentState = "firstPage";
					view.showSearchList();
					view.dataGroup.dataProvider = DataUtil.objToMicroblogs(notification.param["results"]);
					break;
				case PrivateNotify.GET_SEARCH_WEIBO_NEW_RESULT:
					view.currentState = "firstPage";
					view.showSearchList();
					view.dataGroup.dataProvider = DataUtil.objToMicroblogs(notification.param["results"]);
					view.showTotal.text = "共有"+notification.param["total"]+"条微博";
					break;
			}
		
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():SearchView
		{
			return viewComponent as SearchView;
		}
		
		public function searchEvt(e:MouseEvent):void
		{
			if(view.searchType == 0){
				weiboProxy.searchWeiboNew(view.txtInput.text,"0","10");
			}else{
				weiboProxy.searchUser(view.txtInput.text, "0", "10");
			}
		}
		
		
		public function get weiboProxy():WeiboProxy
		{
			return AppFacade.getInstance().retrieveProxy(WeiboProxy.NAME) as WeiboProxy;
		}
	}
}